package com.example.springcloud.gateway.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName : CheckAuthGatewayFilterFactory
 * @Description : 自定义过滤器工厂
 * @Author : tianshuo
 * @Date: 2021-05-17 21:46
 */
@Component
public class CheckAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CheckAuthGatewayFilterFactory.Config> {

    private static final Logger logger = LoggerFactory.getLogger(CheckAuthGatewayFilterFactory.class);


    public CheckAuthGatewayFilterFactory() {
        super(CheckAuthGatewayFilterFactory.Config.class);
    }

    @Override

    public List<String> shortcutFieldOrder() {
        return Arrays.asList("ck");
    }

    @Override
    public GatewayFilter apply(Config config) {

        return (exchange, chain) -> {
            HttpHeaders headers = exchange.getRequest().getHeaders();
            List<String> ck = exchange.getRequest().getQueryParams().get("ck");

            logger.info("增加header:{},参数:{},Config配置：{}",headers.get("ck"),ck,config.getCk());
            return chain.filter(exchange);
        };
    }
    public static class Config {

        // 当Config的属性为ck ，如果要校验header，header中key就为ck
        private String ck;

        public String getCk() {
            return ck;
        }

        public void setCk(String ck) {
            this.ck = ck;
        }
    }

}

